<template>

  <basic-container>
    <el-button @click="mockData">一键mock</el-button>
    <avue-form
      ref="form"
      v-model="form"
      :option="option"
    />
    <bottom-operation
      :show-save-btn="!$route.query.detail"
      :back-list-url="backListUrl"
      @saveClick="(e)=>handleSubmit(e)"
    />
  </basic-container>
</template>
<script>
import { getDetailReq, addInvoice, getTaxList, updateReadInfo, getInvoiceList } from '@/api/invoiceManagement'
import { get as _get, cloneDeep } from 'lodash'
import { myMock } from './myMock.js'
import pnvoiceManageMix from '@/mixins/invoiceManagement'

export default {
  mixins: [pnvoiceManageMix('func')],
  data() {
    return {
      backListUrl: '/invoiceManagement/list',
      pageAttr: {
        isDetail: false,
        isEdit: false,
        isAdd: false
      },
      tempRow: {},
      form: {
        // manageOrg: '',
        businessFieldVoList: [
        ]
      },
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      addStatus: false

    }
  },
  computed: {

  },

  watch: {

  },
  created() {
    const { detail: isDetail, edit: isEdit } = this.$route.query
    this.pageAttr = {
      isDetail: !!isDetail,
      isEdit: !!isEdit,
      isAdd: !!(!isEdit && !isDetail)
    }
    if (isDetail || isEdit) {
      this.option.detail = !!isDetail
      this.getDetail()
    }
    if (this.pageAttr.isAdd) {
      this.form.businessFieldVoList.push(this.defaultFormBody)
    }
  },
  mounted() {

  },
  methods: {
    mockData() {
      let resultObj = Object.assign(this.form, myMock)
      this.$set(this, 'form', resultObj)
    },
    getDetail() {
      getDetailReq({ id: this.$route.query.id }).then(res => {
        let data = _get(res, 'data.data', {})
        this.$set(this, 'form', data || {})
        this.initDicts(data)
      })
    },

    handleGetTaxList(page, data, callback, value) {
      getTaxList(page.currentPage, page.pageSize, { enableStatus: 1, ...data }).then(res => {
        let data = _get(res, 'data.data.records', [])
        callback({
          total: res.data.data.total,
          data: data
        })
      })
    },
    handleGetInvoiceList(page, data, callback, value) {
      getInvoiceList(page.currentPage, page.pageSize, data).then(res => {
        let data = _get(res, 'data.data.records', [])
        callback({
          total: res.data.data.total,
          data: data
        })
      })
    },

    getApis() {
      const { isDetail, isEdit } = this.pageAttr
      let apis = null
      switch (true) {
        case (isDetail):
          apis = ''
          break
        case (isEdit):
          apis = updateReadInfo
          break
        default:
          apis = addInvoice
      }
      return apis
    },
    handleSubmit() {
      print(this.form)
      let apiFunc = this.getApis()
      this.$refs['form'].validate((valid, done) => {
        done()
        if (valid) {
          let params = cloneDeep(this.form)
          params = {
            ...params,
            id: this.$route.query.id || undefined
          }
          apiFunc(params).then(res => {
            if (res.data.code === 200) {
              this.$message.success(res.data.msg)
              this.$router.push(this.backListUrl)
            }
          })
        }
      })
    }
  }
}
</script>
